{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "   第1列  第2列  第3列  第4列\na    1    2    3    4\nb    5    6    7    8\nc    9   10   11   12\nd   13   14   15   16",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>第1列</th>\n      <th>第2列</th>\n      <th>第3列</th>\n      <th>第4列</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>1</td>\n      <td>2</td>\n      <td>3</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>5</td>\n      <td>6</td>\n      <td>7</td>\n      <td>8</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>9</td>\n      <td>10</td>\n      <td>11</td>\n      <td>12</td>\n    </tr>\n    <tr>\n      <th>d</th>\n      <td>13</td>\n      <td>14</td>\n      <td>15</td>\n      <td>16</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 40
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "data = np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]).reshape(4,4)\n",
    "df = pd.DataFrame(data,index=list('abcd'),columns=['第1列','第2列','第3列','第4列'])\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "    第1列  第2列  第3列  第4列    和\na     1    2    3    4   10\nb     5    6    7    8   26\nc     9   10   11   12   42\nd    13   14   15   16   58\n总和   28   32   36   40  136",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>第1列</th>\n      <th>第2列</th>\n      <th>第3列</th>\n      <th>第4列</th>\n      <th>和</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>1</td>\n      <td>2</td>\n      <td>3</td>\n      <td>4</td>\n      <td>10</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>5</td>\n      <td>6</td>\n      <td>7</td>\n      <td>8</td>\n      <td>26</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>9</td>\n      <td>10</td>\n      <td>11</td>\n      <td>12</td>\n      <td>42</td>\n    </tr>\n    <tr>\n      <th>d</th>\n      <td>13</td>\n      <td>14</td>\n      <td>15</td>\n      <td>16</td>\n      <td>58</td>\n    </tr>\n    <tr>\n      <th>总和</th>\n      <td>28</td>\n      <td>32</td>\n      <td>36</td>\n      <td>40</td>\n      <td>136</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 41
    }
   ],
   "source": [
    "df.loc['总和']=df.apply(lambda x:x.sum()) #列\n",
    "df['和']=df.apply(lambda x:x.sum(),axis=1) #行\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "    第1列  第2列  第3列  第4列    和  第6列\na     1    2    3    4   10    3\nb     5    6    7    8   26    7\nc     9   10   11   12   42   19\nd    13   14   15   16   58   27\n总和   28   32   36   40  136   60",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>第1列</th>\n      <th>第2列</th>\n      <th>第3列</th>\n      <th>第4列</th>\n      <th>和</th>\n      <th>第6列</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>1</td>\n      <td>2</td>\n      <td>3</td>\n      <td>4</td>\n      <td>10</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>5</td>\n      <td>6</td>\n      <td>7</td>\n      <td>8</td>\n      <td>26</td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>9</td>\n      <td>10</td>\n      <td>11</td>\n      <td>12</td>\n      <td>42</td>\n      <td>19</td>\n    </tr>\n    <tr>\n      <th>d</th>\n      <td>13</td>\n      <td>14</td>\n      <td>15</td>\n      <td>16</td>\n      <td>58</td>\n      <td>27</td>\n    </tr>\n    <tr>\n      <th>总和</th>\n      <td>28</td>\n      <td>32</td>\n      <td>36</td>\n      <td>40</td>\n      <td>136</td>\n      <td>60</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 42
    }
   ],
   "source": [
    "#当第 3 列数据 > 10 时，第 5 列数据等于第 1 、第 2 列数据之和；否则第 5 列数据等于第 3 列数据\n",
    "df['第6列']=df.apply(lambda x: x[0]+x[1] if x[2]>10 else x[2],axis=1)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}